package com.zpp.friend.service.question.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zpp.common.core.domain.TableDataInfo;
import com.zpp.friend.domain.question.Question;
import com.zpp.friend.domain.question.dto.QuestionQueryDTO;
import com.zpp.friend.domain.question.es.QuestionES;
import com.zpp.friend.domain.question.vo.QuestionVO;
import com.zpp.friend.elasticsearch.QuestionRepository;
import com.zpp.friend.mapper.question.QuestionMapper;
import com.zpp.friend.service.question.IQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionServiceImpl implements IQuestionService {

    @Autowired
    private QuestionRepository questionRepository;

    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public TableDataInfo list(QuestionQueryDTO questionQueryDTO) {
        //1.先从es中查询数据
        long count = questionRepository.count();
        if(count <= 0) {
            //es中无数据，从数据库中刷新数据到es中
            refreshQuestion();
        }
        //此时es中存在数据

        //指定排序方式
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        //配置分页
        Pageable pageable = PageRequest.of(questionQueryDTO.getPageNum() - 1, questionQueryDTO.getPageSize(), sort);

        //获取查询条件信息
        Integer difficulty = questionQueryDTO.getDifficulty();
        String keyWord = questionQueryDTO.getKeyword();

        //es的查询结果
        Page<QuestionES> questionESPage;

        if(difficulty == null && StrUtil.isEmpty(keyWord)) { //查询条件都为空，查询全部信息
            questionESPage = questionRepository.findAll(pageable);

        }else if(difficulty == null) {  //用户未选择困难等级信息
            questionESPage = questionRepository.findByTitleOrContent(keyWord, keyWord, pageable);

        }else if(StrUtil.isEmpty(keyWord)) { //题目和内容信息为空
            questionESPage = questionRepository.findQuestionByDifficulty(difficulty,pageable);

        }else {  //条件都不为空
            questionESPage = questionRepository.findByTitleOrContentAndDifficulty(keyWord, keyWord, difficulty, pageable);
        }

        long total = questionESPage.getTotalElements();
        if(total <= 0) {
            return TableDataInfo.empty();
        }

        List<QuestionES> questionESList = questionESPage.getContent();
        List<QuestionVO> questionVOList = BeanUtil.copyToList(questionESList, QuestionVO.class);

        return TableDataInfo.success(questionVOList, total);
    }


    public void refreshQuestion() {
        //从数据库中查询数据
        List<Question> questionList = questionMapper.selectList(new LambdaQueryWrapper<Question>());
        if(CollectionUtil.isEmpty(questionList)) { //数据库中没数据，直接返回
            return;
        }
        //转化类型再保存到es中
        List<QuestionES> questionESList = BeanUtil.copyToList(questionList, QuestionES.class);
        questionRepository.saveAll(questionESList);
    }
}
